<template>
    <div>
        <el-dialog :title="dialogTitle" :visible.sync="dialogShow" width="800px" :close-on-click-modal="false" :before-close="handleClose">
            <el-form label-width="130px" :model="form" ref="form" :rules="rules">
                <el-form-item label="产品编号">
                    {{ form.code }}
                </el-form-item>
                <el-form-item label="产品名称">
                    {{ form.name }}
                </el-form-item>
                <el-form-item label="审批意见" prop="opinion">
                    <el-input v-model="form.opinion" type="textarea" key="opinion" maxlength="500" :autosize="{ minRows: 3 }" show-word-limit></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer">
                <el-button @click="dialogHandleSubmit(0)">拒绝</el-button>
                <el-button type="primary" @click="dialogHandleSubmit(1)">同意</el-button>
            </div>
        </el-dialog>
    </div>
</template>
<script>
import approveApi from '@/api/store/approve.js';
export default {
    props: ['prodInfo', 'dialogTitle', 'dialogShow'],
    data() {
        return {
            // 表单
            form: {
                opinion: '',
                name: '',
                code: ''
            },
            // 校验规则
            rules: {
                opinion: [
                    {
                        required: true,
                        message: '请输入审批意见',
                        trigger: 'blur'
                    }
                ]
            }
        };
    },
    watch: {
        prodInfo: {
            handler(val) {
                let { id, name, code } = val;
                this.form = Object.assign({ opinion: '' }, JSON.parse(JSON.stringify({ id, name, code })));
            }
        },
        dialogShow: {
            handler(val) {
                if (val) {
                    let { id, name, code } = this.prodInfo;
                    this.form = Object.assign({ opinion: '' }, JSON.parse(JSON.stringify({ id, name, code })));
                }
            }
        }
    },
    methods: {
        // 点击按钮
        dialogHandleSubmit(val) {
            this.$confirm('是否确认' + ['拒绝', '同意'][val] + '通过审批', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            })
                .then(() => {
                    this.$refs.form.validate((valid) => {
                        if (valid) {
                            let { id, opinion } = this.form;
                            approveApi.productApprove({ id, opinion, state: val + 1 }).then((res) => {
                                if (res.data.code != 0) return;
                                this.$message({
                                    type: 'success',
                                    message: '审批成功!'
                                });
                                this.$emit('handleClose');
                                this.$emit('handleUpdate');
                            });
                        }
                    });
                })
                .catch(() => {});
        },
        // 点击关闭
        handleClose() {
            this.$emit('handleClose');
        }
    }
};
</script>
